<?php
class Pageley_Domain_Validator_Length extends Pageley_Domain_Validator_Abstract {
	private $minLength = 0;
	private $maxLength = 4000;
	private $tooLongMessage = "error.validation.tooLong";
	private $tooShortMessage = "error.validation.tooShort";

	public function setMinLength($minLength) {
		$this->minLength = $minLength;
	}

	public function setMaxLength($maxLength) {
		$this->maxLength = $maxLength;
	}

	public function setTooLongMessage($tooLongMessage) {
		$this->tooLongMessage = $tooLongMessage;
	}

	public function setTooShortMessage($tooShortMessage) {
		$this->tooShortMessage = $tooShortMessage;
	}

	public function validate($value, $propertyName, $object) {
		$errors = new Pageley_Util_ArrayMap();
		if($value !== null && $value !== "") {
            $tooShort = $this->rejectIfShorter($value, $this->minLength, $propertyName, $this->tooShortMessage);
    		if($tooShort) {
    			$errors->put($propertyName, $tooShort);
    		}
    		$tooLong = $this->rejectIfLonger($value, $this->maxLength, $propertyName, $this->tooLongMessage);
    		if($tooLong) {
    			$errors->put($propertyName, $tooLong);
    		}
        }
		return $errors;
	}
}